package com.jilefojing.web.base;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import com.jilefojing.beans.constant.Constants;
import com.jilefojing.beans.enums.PlatformEnum;
import com.jilefojing.beans.enums.ServerTypeEnum;
import com.jilefojing.beans.exception.UserContextException;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.context.beans.vo.UserToken;
import com.jilefojing.service.IUserService;
import com.jilefojing.session.service.ILoginService;
import com.jilefojing.session.service.IUserContextService;
import com.jilefojing.session.service.IUserSessionService;
import com.jilefojing.util.CookieUtil;
import com.jilefojing.util.Detect;

@Controller
public class BaseWeb {

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpSession session;
	@Autowired
	private IUserService userInfoService;
	@Autowired
	private IUserContextService userContextService;
	@Autowired
	private IUserSessionService userSessionService;

	@Autowired
	private ILoginService loginService;

	private final static Logger _BASE_LOGGER = LoggerFactory
			.getLogger(BaseWeb.class);

	protected static final String SCHEMA = "http";

	public void setSessionAttribute(String key, Object obj) {
		session.setAttribute(key, obj);
	}

	public void removeSessionAttribute(String key) {
		session.removeAttribute(key);
	}

	public Object getSessionAttribute(String key) {
		return session.getAttribute(key);
	}

	/**
	 * 获取当前用户上下文
	 * 
	 * @param typeEnum
	 * @return
	 * @throws UserContextException
	 */
	public UserContext getUserContext() throws UserContextException {
		return this.getUserContext(getServerTypeEnum());
	}

	/**
	 * 获取系统内存
	 * 
	 * @return
	 */
	public ServerTypeEnum getServerTypeEnum() {
		ServerTypeEnum serverType = this.isMobile() ? ServerTypeEnum.jilefojing_WAP
				: ServerTypeEnum.jilefojing_WEB;
		return serverType;
	}

	/**
	 * 获取当前用户上下文
	 * 
	 * @param typeEnum
	 * @return
	 * @throws UserContextException
	 */
	public UserContext getUserContext(ServerTypeEnum serverType)
			throws UserContextException {
		UserToken userToken = CookieUtil.getUserToken(request);
		// TODO 逻辑待优化
		if (request.getAttribute(Constants.USER_CONTEXT_KEY) != null) {
			return (UserContext) request
					.getAttribute(Constants.USER_CONTEXT_KEY);
		}

		UserContext userContext = userSessionService
				.loadSessionUserContext(userToken);
		request.setAttribute(Constants.USER_CONTEXT_KEY, userContext);
		return userContext;

	}

	public String redirect(String path) {
		return "redirect:" + path;
	}

	/**
	 * 获取当前访问地址(网络)和参数
	 * 
	 * @param request
	 * @return
	 */
	public String getRequestUrl(HttpServletRequest request) {
		String url = request.getRequestURL().toString();
		if (StringUtils.isNotEmpty(request.getQueryString())) {
			url += "?" + request.getQueryString();
		}
		return url;
	}

	@InitBinder
	public void initBinder(ServletRequestDataBinder binder) {
		// 防止xss攻击，并且左右去空格 注:去掉 影响老师解析内容
		// binder.registerCustomEditor(String.class,
		// new StringPropertyEditorSupport(true, true));
	}

	public PlatformEnum getPlatform() {
		PlatformEnum platform = Detect.getPlatform(request
				.getHeader("user-agent"));
		return platform;
	}

	/**
	 * 是否从手机端访问
	 * 
	 * @param request
	 * @return
	 */
	public boolean isMobile() {
		return getPlatform().isMobile();
	}

	/**
	 * 获取客户端访问ip
	 * 
	 * @return
	 */
	public String getClientIp() {
		String ip = request.getHeader("X-Real-IP");
		if (StringUtils.isBlank(ip)) {
			ip = request.getRemoteHost();
		}
		return ip;
	}

}
